import { _decorator, Button, Component, instantiate, Label, Node, Prefab, Sprite, SpriteFrame } from 'cc';
import { RendorManager } from '../Base/RendorManager';
import { ItemStatusEnum, ItemTypeEnum, TriggerStatusEnum, TriggerTypeEnum } from '../Enum';
import { TriggerManager } from './TriggerManager';
import DataManger from '../RunTime/DataManager';

const { ccclass, property } = _decorator;

@ccclass('GrandmaTriggerManager')
export class GrandmaTriggerManager extends TriggerManager{
    type: TriggerTypeEnum = TriggerTypeEnum.Grandma
    
    @property(Node)
    dialogue: Node = null
    
    @property(Label)
    label: Label = null

    private readonly dialoguePending: string[] = [
        `我年纪大了，很多事情想不起来了`,
        `你是谁？算了，我也不在乎你是谁。你能帮我找到信箱的钥匙吗？`,
        `老头子说最近会给我寄船票过来，叫我和他一起出去看看。虽然我没有什么兴趣...`,
        `他折腾了一辈子，不是躲在楼上捣鼓什么时间机器，就是出海找点什么东西。`,
        `这些古怪的电视节目真没有什么意思。`,
        `老头子说这个岛上有很多秘密，其实我知道，不过是岛上的日子太孤独，他找点事情做罢了。`,
        `人嘛，谁没有年轻过。年轻的时候...算了，不说这些往事了。`,
        `老了才明白，万物静默如迷。`, 
    ]

    private readonly dialogueResolved = [
        `没想到老头子的船票寄过来了，谢谢你。`
    ]

    rendor(): void {
        super.rendor()
        if (DataManger.Instance.grandmaDiaIndex === -1) {
            this.dialogue.active = false
            return
        }
        this.dialogue.active = true
        if (DataManger.Instance.grandmaStatus === TriggerStatusEnum.Pending) {
            this.label.string=this.dialoguePending[DataManger.Instance.grandmaDiaIndex]
        }else if (DataManger.Instance.grandmaStatus === TriggerStatusEnum.Resolved) {
            this.label.string=this.dialogueResolved[DataManger.Instance.grandmaDiaIndex]
        }
    }

    handleTrigger() {
        if (DataManger.Instance.grandmaStatus === TriggerStatusEnum.Pending) {
            if (DataManger.Instance.curItemType === ItemTypeEnum.Mail && DataManger.Instance.isSelect) {
                //一些复位：curItem,isSelect
                DataManger.Instance.curItemType = null
                DataManger.Instance.isSelect = false
                //Item状态改变
                DataManger.Instance.items.find(i => i.type === ItemTypeEnum.Mail).status = ItemStatusEnum.Disable
                DataManger.Instance.items = [...DataManger.Instance.items]
        
                DataManger.Instance.grandmaStatus = TriggerStatusEnum.Resolved
                DataManger.Instance.grandmaDiaIndex=0
            } else {
                if (DataManger.Instance.grandmaDiaIndex >= this.dialoguePending.length - 1) {
                    DataManger.Instance.grandmaDiaIndex = -1
                } else {
                    DataManger.Instance.grandmaDiaIndex++
                }
            }
        } else {
            if (DataManger.Instance.grandmaDiaIndex >= this.dialogueResolved.length - 1) {
                DataManger.Instance.grandmaDiaIndex = -1
            } else {
                DataManger.Instance.grandmaDiaIndex++
            }
        }
    }

}